import 'package:flutter/material.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
import 'package:sow_app_core/sow_app_core.dart';

import 'classify_list_tile.dart';

class ClassifyTab extends StatefulWidget {
  final List<Classify> classifyList;

  const ClassifyTab({Key? key, required this.classifyList}) : super(key: key);

  @override
  State<ClassifyTab> createState() => _ClassifyTabState();
}

class _ClassifyTabState extends State<ClassifyTab>
    with AutomaticKeepAliveClientMixin {
  String newClassifyName = '热门';
  int selectedIndex = 0;
  List<Classify> _parentClassifyList = [];

  @override
  Widget build(BuildContext context) {
    super.build(context);

    final ItemScrollController itemScrollController = ItemScrollController();
    final ItemPositionsListener itemPositionsListener =
        ItemPositionsListener.create();

    itemPositionsListener.itemPositions.addListener(() {
      newClassifyName = (_parentClassifyList
          .firstWhere((e) => {e.parentName, e.classifyName}.contains(
              (_parentClassifyList[
                      itemPositionsListener.itemPositions.value.first.index])
                  .parentName))
          .parentName);
      if (_parentClassifyList[selectedIndex].parentName != newClassifyName) {
        setState(() {
          selectedIndex = _parentClassifyList
              .indexWhere((element) => (element.parentName == newClassifyName));
        });
      }
    });
    _parentClassifyList = widget.classifyList
        .where((element) => element != Classify.empty)
        .fold<List<Classify>>([], (acc, cur) {
      acc.indexWhere((e) => e.parentId == cur.parentId) != -1
          ? null
          : acc.add(cur);
      return acc;
    }).toList();
    return Row(
      children: [
        NavigationRail(
          selectedIndex: selectedIndex,
          onDestinationSelected: (int index) {
            setState(() {
              itemScrollController.scrollTo(
                  index: _parentClassifyList.indexWhere((element) =>
                      element.parentId == _parentClassifyList[index].parentId),
                  duration: const Duration(milliseconds: 100));
              selectedIndex = index;
            });
          },
          labelType: NavigationRailLabelType.all,
          destinations: _parentClassifyList
              .map<NavigationRailDestination>((e) => NavigationRailDestination(
                  icon: const SizedBox.shrink(), label: Text(e.parentName)))
              .toList(),
        ),
        Expanded(
          child: ScrollablePositionedList.builder(
            itemCount: _parentClassifyList.length,
            itemBuilder: _scrollablePositionedListBuilder,
            itemPositionsListener: itemPositionsListener,
            itemScrollController: itemScrollController,
          ),
        ),
      ],
    );
  }

  @override
  bool get wantKeepAlive => true;

  Widget _scrollablePositionedListBuilder(BuildContext context, int index) {
    return ClassifyListTile(
      children: widget.classifyList
          .where((element) =>
              element.parentId == _parentClassifyList[index].parentId)
          .toList(),
      parent: _parentClassifyList[index],
    );
  }
}
